package thread;
/**
 * 线程池
 */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest {
    public static void main(String[] args){
        ExecutorService pool= Executors.newFixedThreadPool(4);//设置线程池固定大小
        pool.execute(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (i<5){
                    i++;
                    System.out.println("==>线程1");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (i<5){
                    i++;
                    System.out.println("==>线程2");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (i<5){
                    i++;
                    System.out.println("==>线程3");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (i<5){
                    i++;
                    System.out.println("==>线程4");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while (i<5){
                    i++;
                    System.out.println("==>线程5");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
